package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


//收货地址的业务层实现类
@Service
@Transactional
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private AddressService addressService;

    //查询地址列表(登录用户的地址信息)
    @Override
    public List<Address> findList() {
        //创建条件查询对象
        LambdaQueryWrapper<Address> qw = new LambdaQueryWrapper<>();
        //创建条件查询语句
        qw.eq(Address::getUserId, UserHolder.get().getId());
        qw.orderByDesc(Address::getCreateTime);
        //调用mapper
        return addressMapper.selectList(qw);
    }

    //新增地址
    @Override
    public void save(Address address) {
        //补全对象信息
        //a.补全用户id
        address.setUserId(UserHolder.get().getId());
        //b.补全是否是默认地址

        if (addressService.findList().size() == 0) {
            address.setIsDefault(1);
        } else {
            address.setIsDefault(0);
        }
        //调用mapper新增
        addressMapper.insert(address);
    }

    //地址回显
    @Override
    public Address findById(Long id) {
        //根据id查询
        return addressMapper.selectById(id);
    }

    //修改地址
    @Override
    public void update(Address address) {
        //调用mapper修改
        addressMapper.updateById(address);
    }

    //删除地址
    @Override
    public void delete(List<Long> ids) {
        //创建条件查询对象
        LambdaQueryWrapper<Address> qw = new LambdaQueryWrapper<>();
        //创建条件查询语句
        qw.in(Address::getId, ids);
        //如果删除的地址有默认地址,则将第一个地址设置为默认地址
        List<Address> addressList = addressMapper.selectList(qw);
        //遍历地址列表
        for (Address address : addressList) {
            //判断是否为默认地址
            if (address.getIsDefault() == 1) {
                addressMapper.deleteById(address.getId());
                List<Address> addresses = addressService.findList();
                for (int i = 0; i < addresses.size(); i++) {
                    addresses.get(0).setIsDefault(1);
                    addressMapper.updateById(addresses.get(0));
                    return;
                }
            }
        }
        //调用mapper
        addressMapper.delete(qw);
    }

    //修改默认地址
    @Override
    public void updateDefault(Address address) {
        //创建条件查询对象
        LambdaQueryWrapper<Address> qw = new LambdaQueryWrapper<>();
        //创建条件查询语句
        qw.eq(Address::getUserId, UserHolder.get().getId());
        //创建模范对象
        Address addr = new Address();
        addr.setIsDefault(0);
        //将所有地址都修改为不是默认地址
        addressMapper.update(addr, qw);
        //将传回来的地址设置为默认地址
        address.setIsDefault(1);
        //修改
        addressMapper.updateById(address);
    }

    //查询默认地址
    @Override
    public Address findDefaultAddress() {
        //创建条件查询对象
        LambdaQueryWrapper<Address> qw = new LambdaQueryWrapper<>();
        //创建条件查询语句
        qw.eq(Address::getUserId, UserHolder.get().getId());
        qw.eq(Address::getIsDefault, 1);
        //调用mapper
        Address address = addressMapper.selectOne(qw);
        //返回
        return address;
    }
}
